/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.utilities;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import org.testis.utils.tree.LabelTreeNode;

/**
 *
 * @author selcuk
 */
public class LabelTreeNodeRenderer implements TreeCellRenderer {

    private LabelTreeNode m_labelTreeNode;
    private JCheckBox m_leafRenderer = new JCheckBox();
    private DefaultTreeCellRenderer m_nonLeafRenderer = new DefaultTreeCellRenderer();
    Color m_selectionBorderColor, m_selectionForeground, m_selectionBackground, m_textForeground, m_textBackground;

    public LabelTreeNodeRenderer() {
        Font fontValue;
        fontValue = UIManager.getFont("Tree.font");
        if (fontValue != null) {
            m_leafRenderer.setFont(fontValue);
        }
        Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
        m_leafRenderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));

        m_selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
        m_selectionForeground = UIManager.getColor("Tree.selectionForeground");
        m_selectionBackground = UIManager.getColor("Tree.selectionBackground");
        m_textForeground = UIManager.getColor("Tree.textForeground");
        m_textBackground = UIManager.getColor("Tree.textBackground");
    }

    protected JCheckBox getLeafRenderer() {
        return m_leafRenderer;
    }

    protected LabelTreeNode getLabelTreeNode() {
        return m_labelTreeNode;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component returnValue;
        /*
        if (leaf) {
        String stringValue = tree.convertValueToText(value, selected,
        expanded, leaf, row, false);
        leafRenderer.setText(stringValue);
        leafRenderer.setSelected(false);
        leafRenderer.setEnabled(tree.isEnabled());

        if (selected) {
        leafRenderer.setForeground(selectionForeground);
        leafRenderer.setBackground(selectionBackground);
        } else {
        leafRenderer.setForeground(textForeground);
        leafRenderer.setBackground(textBackground);
        }

        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
        if (userObject instanceof LabelTreeNode) {
        LabelTreeNode node = (LabelTreeNode) userObject;
        leafRenderer.setText(node.getName());
        leafRenderer.setSelected(node.isSelected());
        }
        }
        returnValue = leafRenderer;
        } else {
        returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
        value, selected, expanded, leaf, row, hasFocus);
        }
         */

        String stringValue = tree.convertValueToText(value, selected,
                expanded, leaf, row, false);
        m_leafRenderer.setText(stringValue);
        m_leafRenderer.setSelected(false);
        m_leafRenderer.setEnabled(tree.isEnabled());

        if (selected) {
            m_leafRenderer.setForeground(m_selectionForeground);
            m_leafRenderer.setBackground(m_selectionBackground);
        } else {
            m_leafRenderer.setForeground(m_textForeground);
            m_leafRenderer.setBackground(m_textBackground);
        }

        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof LabelTreeNode) {
                m_labelTreeNode = (LabelTreeNode) userObject;
                m_leafRenderer.setText(m_labelTreeNode.getName());
                m_leafRenderer.setSelected(m_labelTreeNode.isSelected());
            }
        }
//        returnValue = m_leafRenderer;
        return m_leafRenderer;
    }
}
